package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.entity.ProdComm;
import com.powernode.entity.ProdProp;
import com.powernode.entity.ProdTag;
import com.powernode.model.Result;
import com.powernode.service.ProdCommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 商品评论挂你控制层
 */
@Api(tags = "商品评论接口管理")
@RestController
@RequestMapping("prod/prodComm")
public class ProdCommController {

    @Resource
    private ProdCommService prodCommService;


    @ApiOperation("多条件分页查询商品评论")
    @GetMapping("page")
    @PreAuthorize("hasAnyAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String propName,
                                                   @RequestParam(required = false) Integer status){

        Page<ProdComm> page = new Page<>(current, size);

        page = prodCommService.page(page,new LambdaQueryWrapper<ProdComm>()
                .eq(ObjectUtil.isNotEmpty(status),ProdComm::getStatus,status)
                .like(!propName.isEmpty(),ProdComm::getProdName,propName)
                .orderByDesc(ProdComm::getCreateTime));

        return Result.success(page);
    }


    @ApiOperation("根据标识查询商品评论详情")
    @GetMapping("{id}")
    @PreAuthorize("hasAnyAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@PathVariable Long id){
        ProdComm prodComm = prodCommService.getById(id);
        return Result.success(prodComm);
    }

    @ApiOperation("回复和审核商品评论")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('prod:prodComm:update')")
    public Result<String> replyAndExamineProdComm(@RequestBody ProdComm prodComm){
        Boolean flag = prodCommService.replyAndExamineProdComm(prodComm);
        return Result.handle(flag);
    }




}
